package com.moleq.service;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.models.DvrSetting;
import com.moleq.models.GlobalInfo;
import com.moleq.models.ImportData;
import com.moleq.models.InventorySetting;

public class DvrSettingService {
	Context mContext;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;

	public DvrSettingService(Context context) {
		mContext = context;
		this.DBHelper = new DatabaseHelper(GlobalInfo.getInstance().ShareContext);
	}

	public DvrSetting FindData() {
		String sql = "select * from "
				+ DatabaseSchema.DvrSetting.SCHEMA_NAME;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		DvrSetting mDvrSetting = null;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			return null;
		}
		if (cursor.moveToNext()) {
			mDvrSetting = new DvrSetting();
			mDvrSetting.setDs_id(cursor.getColumnIndex(DatabaseSchema.DvrSetting.DS_ID));
			mDvrSetting.setDs_onoff(cursor.getString(cursor.getColumnIndex(DatabaseSchema.DvrSetting.DS_ONOFF)));
			mDvrSetting.setDs_dvr_ip(cursor.getString(cursor.getColumnIndex(DatabaseSchema.DvrSetting.DS_DVR_IP)));
			mDvrSetting.setDs_port(cursor.getInt(cursor.getColumnIndex(DatabaseSchema.DvrSetting.DS_PORT)));
			cursor.close();
			return mDvrSetting;
		}
		cursor.close();
		return mDvrSetting;
	}

	public void Insert(DvrSetting object) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("INSERT INTO " + DatabaseSchema.DvrSetting.SCHEMA_NAME
				+ "(" + DatabaseSchema.DvrSetting.DS_ID + ","
				+ DatabaseSchema.DvrSetting.DS_ONOFF + ","
				+ DatabaseSchema.DvrSetting.DS_DVR_IP + ","
				+ DatabaseSchema.DvrSetting.DS_PORT + ")"
				+ " VALUES(?,?,?,?)", new Object[] { null, object.getDs_onoff(),
				object.getDs_dvr_ip(),object.getDs_port() });
	}

	public int Update(DvrSetting object) {
		String sql = "update " + DatabaseSchema.DvrSetting.SCHEMA_NAME
				+ " set " + DatabaseSchema.DvrSetting.DS_ONOFF + " = '"
				+ object.getDs_onoff() + "',"
				+ DatabaseSchema.DvrSetting.DS_DVR_IP + " = '"
				+ object.getDs_dvr_ip() +"',"
				+ DatabaseSchema.DvrSetting.DS_PORT + " = "
				+ object.getDs_port() + " where 1 = 1 ";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(sql);

		return 1;
	}

	public boolean Delete() {
		try {
			db = DBHelper.getWritableDatabase();
			db.execSQL("Delete from "
					+ DatabaseSchema.DvrSetting.SCHEMA_NAME);
		} catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		return true;
	}
}
